package cn.yeyiu.sexyiu.service

import cn.yeyiu.sexyiu.bean.MangaInfo
import cn.yeyiu.sexyiu.data.VideoConfig
import com.alibaba.fastjson.JSONObject
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.io.File
import java.io.FileInputStream
import java.io.IOException

@Service
class FileService {

    @Value("\${config.path}")
    lateinit var PATH: String

    private fun getSexyiuFile(sexyiuType: String, detailType: String, mediaType: String, fileName: String): File {
        val filePath = String.format("%s/%s/%s/%s/%s", PATH, sexyiuType, detailType, mediaType, fileName)
        return File(filePath)
    }

    fun getFiles(path: String): Array<File> {
        return File(path).listFiles()
    }

    fun getVideoFile(sexyiuType: String, detailType: String, fileName: String): File {
        return getSexyiuFile(sexyiuType, detailType, "GB", fileName)
    }

    fun getImgFile(sexyiuType: String, detailType: String, fileName: String): File {
        return getSexyiuFile(sexyiuType, detailType, "IMG", fileName)
    }

    fun getAnimeVideoFile(detailType: String, fileName: String): File {
        return getVideoFile(VideoConfig.ANIME, detailType, fileName)
    }

    fun getAnimeImgFile(detailType: String, fileName: String): File {
        return getImgFile(VideoConfig.ANIME, detailType, fileName)
    }

    fun getOtherVideoFile(detailType: String, fileName: String): File {
        return getVideoFile(VideoConfig.OTHER, detailType, fileName)
    }

    fun getOtherImgFile(detailType: String, fileName: String): File {
        return getImgFile(VideoConfig.OTHER, detailType, fileName)
    }

    fun getMangaFile(title: String, ep: String, idx: String): File {
        val filePath = PATH + VideoConfig.MANGA + "/" + title + "/eps/" + ep + "/" + idx
        return File(filePath)
    }

    fun getMangaLogo(title: String): File {
        val filePath = PATH + VideoConfig.MANGA + "/" + title + "/logo"
        return File(filePath)
    }

    fun getMangaEpLogo(title: String, ep: String): File {
        val filePath = PATH + VideoConfig.MANGA + "/" + title + "/eps/" + ep + ".logo"
        return File(filePath)
    }

    fun listFileNames(path: String): Array<String> {
        return File(path).list()
    }

    fun listOtherVideoNames(type: String): Array<String> {
        val path = PATH + VideoConfig.OTHER + "/" + type + "/GB"
        return listFileNames(path)
    }

    fun listAnimeVideoNames(serial: String): Array<String> {
        val path = PATH + VideoConfig.ANIME + "/" + serial + "/GB/"
        return listFileNames(path)
    }

    fun listAnimeImgNames(serial: String): Array<String> {
        val path = PATH + VideoConfig.ANIME + "/" + serial + "/IMG/"
        return listFileNames(path)
    }

    fun listMangaNames(): Array<String> {
        val path = PATH + VideoConfig.MANGA
        return listFileNames(path)
    }

    fun getMangaInfo(name: String): MangaInfo? {
        try {
            val file = PATH + VideoConfig.MANGA + "/" + name + "/info.json"
            val info = String(FileInputStream(file).readAllBytes())
            return JSONObject.parseObject(info, MangaInfo::class.java)
        } catch (ignore: IOException) {
        }
        return null
    }
}